package com.meiyu.live.tv.anchor;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.response.PrecloseResponse;
import com.meiyu.live.popj.PersonalInfo;
import com.meiyu.live.util.ImageUtil;

import java.text.SimpleDateFormat;

public class AnchorFinishActivity extends BaseActivity implements View.OnClickListener {

    public final static String AUTHOR_NAME = "author_name";
    public final static String AUTHOR_AVATAR = "author_avatar";
    public final static String AUDIENCE_COUNT = "audience_count";

    private ImageLoader imageLoader;
    private DisplayImageOptions options_avatar;

    private ImageView iv_bg, iv_avatar;
    private TextView tv_audience_count, tv_live_time, tv_like_count, tv_getcoin;
    private View btn_back, btn_close;

    private String author_name; //主播名字
    private String author_avatar; //主播头像
    private String audience_count;//观看人数

    private Bitmap bitmapAvatarBlur;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anchorfinish);
        author_name = getIntent().getStringExtra(AUTHOR_NAME);
        author_avatar = getIntent().getStringExtra(AUTHOR_AVATAR);
        audience_count = getIntent().getStringExtra(AUDIENCE_COUNT);
        PersonalInfo personalInfo = UserConfig.getInstance().getPersonalInfo();
        if (personalInfo != null) {
            author_name = personalInfo.getName();
            author_avatar = personalInfo.getAvatar();
        }
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void init() {
        imageLoader = ImageLoader.getInstance();
        options_avatar = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_def_header)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.icon_def_header)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.icon_def_header)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();

        iv_bg = (ImageView) findViewById(R.id.iv_bg);
        iv_avatar = (ImageView) findViewById(R.id.iv_avatar);
        tv_audience_count = (TextView) findViewById(R.id.tv_audience_count);
        tv_live_time = (TextView) findViewById(R.id.tv_live_time);
        tv_like_count = (TextView) findViewById(R.id.tv_like_count);
        tv_getcoin = (TextView) findViewById(R.id.tv_getcoin);

        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        btn_close = findViewById(R.id.btn_close);
        btn_close.setOnClickListener(this);

        update();
        getData();
    }

    private void update() {
        imageLoader.displayImage(author_avatar, iv_avatar, options_avatar, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, final Bitmap bitmap) {
                if (bitmap != null) {
                    if (Build.VERSION.SDK_INT >= 17) {
                        if (bitmapAvatarBlur != null && !bitmapAvatarBlur.isRecycled()) {
                            bitmapAvatarBlur.recycle();
                            iv_bg.setImageResource(R.color.black);
                        }
                        try {
                            bitmapAvatarBlur = ImageUtil.blurBitmap(AnchorFinishActivity.this, bitmap);
                            iv_bg.setImageBitmap(bitmapAvatarBlur);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        if (bitmapAvatarBlur != null && !bitmapAvatarBlur.isRecycled()) {
                            bitmapAvatarBlur.recycle();
                            iv_bg.setImageResource(R.color.black);
                        }
                        new Thread() {
                            @Override
                            public void run() {
                                bitmapAvatarBlur = ImageUtil.blurBitmapOld(AnchorFinishActivity.this, bitmap);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        iv_bg.setImageBitmap(bitmapAvatarBlur);
                                    }
                                });
                            }
                        }.start();
                    }
                }
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });
    }

    private void getData() {
        BaseBodyParams b = new BaseBodyParams();
        b.setUrl(Constant.UrlParams.PRECLOSE);
        request(b, PrecloseResponse.class, new Listener<PrecloseResponse>() {
            @Override
            public void onResponse(PrecloseResponse precloseResponse) {
                if (precloseResponse != null) {
                    tv_audience_count.setText(precloseResponse.getViewer_num() + "");
                    int duration = (int) precloseResponse.getDuration();
                    int mm = duration / 60;
                    int ss = duration - mm * 60;
                    tv_live_time.setText((mm > 9 ? "" : "0") + mm + ":" + (ss > 9 ? "" : "0") + ss);
//                    tv_live_time.setText(dateFormat.format(precloseResponse.getDuration() * 1000));
                    tv_like_count.setText(precloseResponse.getGetfansduring() + "");
                    tv_getcoin.setText(precloseResponse.getBenefit() + "");
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            setResult(RESULT_OK);
            finish();
        } else if (view == btn_close) {
            setResult(RESULT_CANCELED);
            finish();
        }
    }
}
